package com.uzai.console.service.sop.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.entity.User;
import com.uzai.console.dto.sop.sopprirewardruleuser.SopPriRewardRuleUserQuery;
import com.uzai.console.entity.Device;
import com.uzai.console.entity.SopPriRewardRuleStrategy;
import com.uzai.console.entity.SopPriRewardRuleUser;
import com.uzai.console.mapper.SopPriRewardRuleStrategyMapper;
import com.uzai.console.mapper.SopPriRewardRuleUserMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.sop.SopPriRewardRuleUserService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.vo.sop.sopprirewardruleuser.SopPriRewardRuleUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * SOP任务
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
@Slf4j
public class SopPriRewardRuleUserServiceImpl implements SopPriRewardRuleUserService {

    @Autowired
    private SopPriRewardRuleUserMapper sopPriRewardRuleUserMapper;
    @Autowired
    private SopPriRewardRuleStrategyMapper sopPriRewardRuleStrategyMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceService deviceService;

    /**
     * 查询调整会员信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return SopPriRewardRuleUserVo
     */
    public Page<SopPriRewardRuleUserVo> findByPage(SopPriRewardRuleUserQuery sopPriRewardRuleUserQuery){

        //新建返回到页面的LIST对象
        List<SopPriRewardRuleUserVo> sopPriRewardRuleUserVoList = new ArrayList<>();

        //执行分页查询对象
        Page<SopPriRewardRuleUser> page = new Page<>();
        sopPriRewardRuleUserQuery.convert(page);
        List<SopPriRewardRuleUser> sopPriRewardRuleUserList = sopPriRewardRuleUserMapper.findByList(page, sopPriRewardRuleUserQuery);
        if(sopPriRewardRuleUserList != null){
            for (SopPriRewardRuleUser sopPriRewardRuleUser : sopPriRewardRuleUserList){
                SopPriRewardRuleUserVo sopPriRewardRuleUserVo = new SopPriRewardRuleUserVo();
                BeanUtils.copyProperties(sopPriRewardRuleUser, sopPriRewardRuleUserVo);

                //策略名称
                if(sopPriRewardRuleUser.getStrategyId() != null){
                    SopPriRewardRuleStrategy sopPriRewardRuleUserStrategy = sopPriRewardRuleStrategyMapper.selectById(sopPriRewardRuleUser.getStrategyId(), sopPriRewardRuleUser.getMerId());
                    if(sopPriRewardRuleUserStrategy != null){
                        sopPriRewardRuleUserVo.setStrategyName(sopPriRewardRuleUserStrategy.getName());
                    }
                }

                //会员信息
                User userInfo = userService.findUserInfo(sopPriRewardRuleUser.getUserId(), sopPriRewardRuleUser.getDeviceType(), sopPriRewardRuleUser.getMerId());
                if(userInfo != null){
                    sopPriRewardRuleUserVo.setUserName(userInfo.getNickName());
                }

                //机器人信息
                Device device = deviceService.findDeviceInfoByDeviceUniqueId(sopPriRewardRuleUser.getDeviceUniqueId(), sopPriRewardRuleUser.getDeviceType(), sopPriRewardRuleUser.getMerId());
                if(device != null){
                    sopPriRewardRuleUserVo.setDeviceName(device.getDeviceName());
                    sopPriRewardRuleUserVo.setDeviceId(device.getDeviceId());
                    sopPriRewardRuleUserVo.setDeviceRemark(device.getDeviceRemark());
                }
                sopPriRewardRuleUserVoList.add(sopPriRewardRuleUserVo);
            }
        }
        //返回到页面分页对象
        Page<SopPriRewardRuleUserVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(sopPriRewardRuleUserVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    

}
